# -*- coding: utf-8 -*-
# python2.7环境
import paramiko
import subprocess
import os
import ConfigParser
def ssh_scpHtmlfile():
    # 创建文件传
    commands = "zip -r dist.zip ./dist;"
    os.system(commands)
    print("准备部署====================================")
    # 准备部署
    localFile = r'./dist.zip'
    targetFile = r'/usr/local/source/fish-networking-frontend/dist.zip'
    transport=paramiko.Transport(('121.40.95.119',22))
    # 获取上一个文件夹的路径
    parent_dir = os.path.dirname(os.getcwd())
    config = ConfigParser.ConfigParser()
    config.read(os.path.join(parent_dir, 'local.properties'))
    pwd = config.get('121.40.95.119', 'server_pwd')
    transport.connect(username='root',password=pwd)
    # 实例化SSH对象,先操作文件脚本
    ssh = paramiko.SSHClient()
    ssh._transport = transport
    # 先处理文件
    cmd = 'cd /usr/local/source/fish-networking-frontend;rm -rf dist;'
    ssh.exec_command(cmd)
    #文件上传
    sftp=paramiko.SFTPClient.from_transport(transport)
    sftp.put(localFile,targetFile)  #上传
    # 解压zip文件
    zipCmd = 'cd /usr/local/source/fish-networking-frontend;unzip dist.zip;rm -rf dist.zip;'
    # 打印数据
    stdin, stdout, stderr = ssh.exec_command(zipCmd)
    print(stdout.read().decode('utf-8'))
    transport.close()
    print("部署完成====================================")
if __name__=='__main__':
    ssh_scpHtmlfile()
